import 'package:get/get.dart';
import 'package:tencent_cloud_chat_sdk/enum/message_elem_type.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.dart';
import 'package:vischat/core/utils/tencent/message_util.dart';
import 'package:vischat/modules/home/socialize/chat/chat_controller.dart';

class MessageSendController extends GetxController {
  final ChatController _chatController;

  MessageSendController(this._chatController);

  /// 重发
  Future<void> resend(V2TimMessage msg) async {
    switch (msg.elemType) {
      case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
        await sendText(msg.textElem?.text ?? '');
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
        await sendImage(msg.imageElem?.path ?? '');
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_FILE:
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_FACE:
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_MERGER:
        break;
      default:
        break;
    }
  }

  /// 发送文字消息
  Future<void> sendText(String text) async {
    String id = await MessageUtil.createTextMessage(text: text);
    await sendMessage(id);
  }

  /// 发送图片消息
  Future<void> sendImage(String imagePath) async {
    String id = await MessageUtil.createImageMessage(imagePath: imagePath);
    await sendMessage(id);
  }

  Future<void> sendMessage(String id) async {
    await MessageUtil.sendMessage(
      id: id,
      receiver: _chatController.conversation.userID ?? '',
      groupID: _chatController.conversation.groupID ?? '',
      callBack: (msg) {
        if (msg != null) {
          _chatController.addReceivedMessage(msg);
        }
      },
    );
  }
}
